<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="60%" @confirm="handleSubmit" @close="handleClose">
            <div class="flex">
                <div class="flex-1">
                    <el-upload class="upload-demo" drag :action="importUploadUrl" :headers="headers" :on-success="handleSuccess" multiple>
                        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                        <div class="el-upload__text">Drop file here or <em>click to upload</em></div>
                        <template #tip>
                            <div class="el-upload__tip">jpg/png files with a size less than 500kb</div>
                            <a href="https://ihjmf.com/tpl/卡密导入模板.xlsx" target="_blank">点击下载模板</a>
                        </template>
                    </el-upload>
                </div>
                <div class="flex-1">
                    <el-form ref="form" label-width="80px" :inline="false" size="normal">
                        <el-form-item label="归属商户" prop="merchant_id">
                            <el-select v-model="formData.merchant_id" value-key="" placeholder="" clearable filterable>
                                <el-option v-for="item in state.merchantList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="产品" prop="product_id">
                            <el-checkbox-group v-model="formData.product_type_id_arr">
                                <el-checkbox v-for="item in state.items" :key="item.id" :label="item.id">
                                    {{ item.name }}
                                </el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-form>
                </div>
            </div>

            <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
                <el-tab-pane label="正常" name="first"></el-tab-pane>
                <el-tab-pane label="错误" name="second"></el-tab-pane>
            </el-tabs>

            <el-table v-if="activeName === 'first'" :data="state.normal" border stripe max-height="600px">
                <el-table-column label="手机号" prop="mobile" />
                <el-table-column label="姓名" prop="name" />
            </el-table>

            <el-table v-if="activeName === 'second'" :data="state.error" border stripe max-height="600px">
                <el-table-column label="手机号" prop="mobile" />
                <el-table-column label="姓名" prop="name" />
                <el-table-column label="错误信息" prop="error_msg" width="200" />
            </el-table>
        </popup>
    </div>
</template>

<script lang="ts" setup name="cardEdit">
import { apiCardImporAdd } from '@/api/card'
import type { TabsPaneContext } from 'element-plus'

import { apiProductTypeAll } from '@/api/product_type'
import { apiMerchantAll } from '@/api/merchant'
const activeName = ref('first')

const handleClick = (tab: TabsPaneContext, event: Event) => {
    console.log(tab, event)
}

import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import type { PropType } from 'vue'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()

import { ref } from 'vue'
import { genFileId } from 'element-plus'
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'

const upload = ref<UploadInstance>()
import useUserStore from '@/stores/modules/user'
import config from '@/config'
const userStore = useUserStore()
const headers = computed(() => ({
    token: userStore.token,
    version: config.version
}))
const importUploadUrl = ref(`${config.baseUrl}${config.urlPrefix}/card/import`)

const handleSuccess = (data: any) => {
    state.normal = data.data.normal
    state.error = data.data.error
}

// 弹窗标题
const popupTitle = computed(() => {
    return '导入自助卡'
})

const state = reactive({
    normal: [],
    error: [],
    items: [],
    merchantList: []
})

// 表单数据
const formData = reactive({
    userList: [],
    merchant_id: 0,
    product_type_id_arr: []
})

const handleSubmit = async () => {
    formData.userList = state.normal
    await apiCardImporAdd(formData)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    popupRef.value?.open()
    state.normal = []
    state.error = []
    formData.merchant_id = 0
    formData.product_type_id_arr = []
    apiProductTypeAll({}).then((res) => {
        state.items = res
    })
    apiMerchantAll({}).then((res) => {
        state.merchantList = res
    })
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

defineExpose({
    open
})
</script>
